
/**
 * AppleIIGo
 * Bit macros
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#ifndef BITMACROS_H

#define maskWithBit7(value, bit)	(((value) & 0x7f) | ((bit) << 7))
#define setBit(reg, index, bit)		(reg = bit ? \
	((reg) | (index)) : ((reg) & ~(index)))
#define getBit(reg, index)			(((reg) & (index)) != 0)

#define setBigEndianUINT16(p, v)	((UINT8 *) p)[0] = (((v) >> 8) & 0xff);\
									((UINT8 *) p)[1] = (((v) >> 0) & 0xff)
#define getBigEndianUINT16(p)		((((UINT8 *) p)[0] << 8) | \
									(((UINT8 *) p)[1] << 0))

#define setBigEndianUINT32(p, v)	((UINT8 *) p)[0] = (((v) >> 24) & 0xff);\
									((UINT8 *) p)[1] = (((v) >> 16) & 0xff);\
									((UINT8 *) p)[2] = (((v) >> 8) & 0xff);\
									((UINT8 *) p)[3] = (((v) >> 0) & 0xff)
#define getBigEndianUINT32(p)		((((UINT8 *) p)[0] << 24) | \
									(((UINT8 *) p)[1] << 16) | \
									(((UINT8 *) p)[2] << 8) | \
									(((UINT8 *) p)[3] << 0))

#define setLittleEndianUINT16(p, v)	((UINT8 *) p)[1] = (((v) >> 8) & 0xff);\
									((UINT8 *) p)[0] = (((v) >> 0) & 0xff)
#define getLittleEndianUINT16(p)	((((UINT8 *) p)[1] << 8) | \
									(((UINT8 *) p)[0] << 0))

#define setLittleEndianUINT32(p, v)	((UINT8 *) p)[3] = (((v) >> 24) & 0xff);\
									((UINT8 *) p)[2] = (((v) >> 16) & 0xff);\
									((UINT8 *) p)[1] = (((v) >> 8) & 0xff);\
									((UINT8 *) p)[0] = (((v) >> 0) & 0xff)
#define getLittleEndianUINT32(p)	((((UINT8 *) p)[3] << 24) | \
									(((UINT8 *) p)[2] << 16) | \
									(((UINT8 *) p)[1] << 8) | \
									(((UINT8 *) p)[0] << 0))

#endif
